using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace TimeWar
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Dalek : TimeWarSceneItem
    {
        protected Game gameObj = null;

        // Set the 3D model to draw.
        protected Model dalekModel;

        // The aspect ratio determines how to scale 3d to 2d projection.
        protected float aspectRatio;

        // Set the position of the model in world space, and set the rotation.
        protected Vector3 modelPosition = Vector3.Zero;
        protected float modelRotation = 0.0f;

        // Set the position of the camera in world space, for our view matrix.
        protected Vector3 cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f);

        protected void LoadContent()
        {
            
            dalekModel = gameObj.Content.Load<Model>("Content\\Models\\Dalek");
            //dalekModel = gameObj.Content.Load<Model>("Content\\Models\\Sovereign");

            //aspectRatio = (float)gameObj.GraphicsDevice.Viewport.Width / (float)gameObj.GraphicsDevice.Viewport.Height;
            aspectRatio = 16 / 9;
        }

        public Dalek(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
            gameObj = game;
            LoadContent();
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public void Initialize()
        {
            // TODO: Add your initialization code here

            //base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void  Update(TimeSpan time, TimeSpan elapsedTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                gameObj.Exit();
            }

            modelRotation += (float)elapsedTime.TotalMilliseconds * MathHelper.ToRadians(0.1f);
            //modelRotation = 270;

        }



        public void Draw()
        {
            gameObj.GraphicsDevice.Clear(Color.CornflowerBlue);

            // Copy any parent transforms.
            Matrix[] transforms = new Matrix[dalekModel.Bones.Count];
            dalekModel.CopyAbsoluteBoneTransformsTo(transforms);

            // Draw the model. A model can have multiple meshes, so loop.
            foreach (ModelMesh mesh in dalekModel.Meshes)
            {
                // This is where the mesh orientation is set, as well as our camera and projection.
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation) * Matrix.CreateTranslation(modelPosition);
                    effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Forward);
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f);
                }
                // Draw the mesh, using the effects set above.
                mesh.Draw();
            }
        }
    }
}